package com.wbs.shop.nuby.view.base;

import android.os.Bundle;
import androidx.annotation.NonNull;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by zhoucl on 2016/11/3.
 */
public class BaseViewModel<T> implements LifecycleProvider {
    protected final BehaviorSubject<LifecycleEvent> lifeSubject = BehaviorSubject.create();
    WeakReference<T> mView;
    private CompositeDisposable mCompositeDisposable;
    public BehaviorSubject<LifecycleEvent> getLifeSubject() {
        return lifeSubject;
    }
    public BaseViewModel(){
    }

    public void onBind(@NonNull T view){
        mView = new WeakReference<>(view);
        mCompositeDisposable = new CompositeDisposable();
    }
    public void add(Disposable d) {
        if (mCompositeDisposable != null)
            mCompositeDisposable.add(d);
    }
    public void onResume() {

    }

    public void onSaveInstanceState(Bundle outState) {

    }
    public void onPause() {

    }

    public void onStop() {

    }

    public void onDestroy() {
        lifeSubject.onNext(LifecycleEvent.DESTROY);
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }
    public void onBackPressed(){

    }


    public T getView()
    {
        return mView.get();
    }


}
